/************************************************************************
This file is part of Polar4linux
Copyright (C) 2010-2011 Eric jrdn2
Portions Copyright (C) 2007 Matti Juvonen <mpj@iki.fi> (RS400 Tools)
Portions Copyright (C) 2006 Jani Hursti <jani.hursti@hut.fi>,
Portions Copyright (C) 2001-2003 Tom Oliveira e Silva

This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
as published by the Free Software Foundation; either version 2
of the License, or (at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
************************************************************************/

#include "polarConnexion.h"

polarConnexion::polarConnexion(polar4linuxInternals *internals0)
{
	internals=internals0;
	setupUi(this);
	connect(pushButtonOK, SIGNAL(clicked()), this, SLOT(initIRDA()));
	connect(pushButtonCancel, SIGNAL(clicked()), this, SLOT(giveup()));
	statusScene = new QGraphicsScene(this);
	statusView->setScene(statusScene);
	//ithread=NULL;
}

void polarConnexion::giveup()
{
	internals->IRDAok=0;
	drawStatus("");
	reject();
}

void polarConnexion::drawStatus(QString message)
{
	labelMessage->setText(message);
	gBrush.setStyle(Qt::SolidPattern);
	if (internals->IRDAok==1) gBrush.setColor(Qt::green);
	else gBrush.setColor(Qt::red);
	statusView->scene()->addEllipse(QRectF(0, 0, 10, 10), gPen, gBrush);
	show();
	QApplication::processEvents();
}

void polarConnexion::initIRDA()
{
	setCursor(Qt::WaitCursor);
	if (internals->IRDAok!=1)
	{
		internals->IRDAok=init_session(internals->ircommPath.toStdString(),internals->connexionTimeout);
	}
	setCursor(Qt::ArrowCursor);
	if (internals->IRDAok==1)
	{
		if (expectedSignal==-1) emit notConnected();
		else if (expectedSignal==0) emit connexionDone();
		else if (expectedSignal==1) emit readyGet();
		else if (expectedSignal==2) emit readySet();
		else if (expectedSignal==3) emit readyDelete1();
		else if (expectedSignal==4) emit readyDeleteAll();
		else emit notConnected();
	}
}

//set the watch to idle and close port
void polarConnexion::closeIRDA()
{
	if (internals->IRDAok)
	{
		RS400_close_session();
		internals->IRDAok = 0;
		emit notConnected();
	}
}


bool polarConnexion::checkConnexion(int _expectedSignal)
{
	if (internals->IRDAok==0)
	{
		setProgress(0);
		expectedSignal=_expectedSignal;
		drawStatus(tr("Prepare the watch and press Connect"));
		exec();
		return result();
	}
	return true;
}


void polarConnexion::setMaximum(int value)
{
	progressBarDownload->setMaximum(value);
}

void polarConnexion::setProgress(int value)
{
	progressBarDownload->setValue(value);
}
